package advert_api

import (
	"github.com/gin-gonic/gin"
	"zswei.xyz/manager/global"
	"zswei.xyz/manager/models"
	"zswei.xyz/manager/models/res"
)

type AdvertUploadRequest struct {
	ID     int    `json:"id" binding:"required" desc:"请选择广告ID"`
	Title  string `json:"title" binding:"required" desc:"请输入广告名称"`
	Href   string `json:"href" binding:"required" desc:"请输入广告跳转地址"`
	Images string `json:"images" binding:"required" desc:"请输入广告展示图"`
	IsShow *bool  `json:"is_show" binding:"required" desc:"请选择是否展示"`
}

// AdvertUpdateView 修改广告
//
//	@Tags			广告管理
//	@Summary		修改广告
//	@Description	修改广告接口
//	@Param			data	body	AdvertUploadRequest	true	"修改广告参数"
//	@Router			/v2/advert/updateAdvert [put]
//	@Produce		json
//	@Success		200	{object}	res.Response{}
func (AdvertApi) AdvertUpdateView(c *gin.Context) {
	var cr AdvertUploadRequest
	err := c.ShouldBindJSON(&cr)
	if err != nil {
		res.FailWithErrorDesc(err, &cr, c)
		return
	}

	//	查看广告是否存在
	var advertModel models.AdvertModel
	err = global.DB.Take(&advertModel, cr.ID).Error
	if err != nil {
		res.FailWithDesc("广告不存在", c)
		return
	}

	//	修改广告，字段多了不好操作，使用第三方包结构体转map
	err = global.DB.Model(&advertModel).Updates(map[string]any{
		"title":   cr.Title,
		"href":    cr.Href,
		"images":  cr.Images,
		"is_show": cr.IsShow,
	}).Error
	if err != nil {
		res.FailWithDesc("修改广告失败", c)
		return
	}

	res.OkWith(c)
}
